using System;
using Microsoft.SPOT;
using System.Text;

namespace Dairy_Town.Models
{
    /// <summary>
    /// Class to bundle the information from a particular "snapshot" of a vehicle's behavior.
    /// </summary>
    class Snapshot
    {
        public int Entry { get; set; }
        public double Fuel { get; set; }                      // Fuel level of the vehicle.
        public DateTime GPSFix { get; set; }
        public double Heading { get; set; }                   // Heading direction of the GPS module (ranges from 0-360 degrees)
        public double Latitude { get; set; }                       // Latitude coordinate retrieved from GPS module.
        public double Longitude { get; set; }                       // Longitude coordinate retrieved from GPS module.
        public double XAcceleration { get; set; }
        public double YAcceleration { get; set; }
        public double ZAcceleration { get; set; }
        public DateTime Recorded { get; set; }
        public double RPM { get; set; }                       // RPM of vehicle.
        public double Speed { get; set; }                     // Speed of vehicle.
        public double Throttle { get; set; }                  // Throttle position of the vehicle (ranges from 0-100%)
        public string VIN { get; set; }
        public string Version { get; set; }

        public Snapshot()
        {
        }

        public override string ToString()
        {
            string[] keys = { "Entry", "Fuel", "GPSFix", "Heading", "Latitude", "Longitude", "XAcceleration", "YAcceleration", "ZAcceleration", "Recorded", "RPM", "Speed", "Throttle", "VIN", "Version" };

            StringBuilder sb = new StringBuilder("{");
            foreach (string key in keys)
            {
                string value = "";

                string sep = (key == keys[0]) ? "" : ",";

                if (key == "Speed")
                    Debug.Print("");

                sb.Append(sep);
                sb.Append("\"" + key + "\"");
                sb.Append(": ");

                try
                {
                    System.Reflection.MethodInfo method = this.GetType().GetMethod("get_" + key);
                    object obj = method.Invoke(this, null);
                    if (obj.GetType() == typeof(double))
                        value = ((double)obj).ToString("F3");
                    else if (obj.GetType() == typeof(int))
                        value = ((int)obj).ToString();
                    else if (obj.GetType() == typeof(DateTime))
                        value = "\"" + ((DateTime)obj).ToString("MM-dd-yyyy H:mm:ss") + "\"";
                    else
                        value = "\"" + obj.ToString() + "\"";

                    sb.Append(value);
                }
                catch (System.NullReferenceException ex)
                {
                    sb.Append("null");
                    Debug.Print(key + " caused an exception. " + ex.Message);
                }
            }
            sb.Append("}");

            return sb.ToString();
        }
    }
}
